package com.venutech.gongan.api;

import com.baomidou.mybatisplus.plugins.Page;
import com.ruoyi.project.system.domain.SysUser;
import com.venutech.gongan.common.Constants;
import com.venutech.gongan.common.Messages;
import com.venutech.gongan.entity.Jkxt;
import com.venutech.gongan.entity.RzLog;
import com.venutech.gongan.model.ResultFormat;
import com.venutech.gongan.service.JkxtService;
import com.venutech.gongan.service.RzLogService;
import com.venutech.gongan.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = {"监控系统控制层接口"})
@RestController
@RequestMapping("jkxt")
public class JkxtController extends AbstractControllerBase {
    @Resource
    private JkxtService jkxtService;
    @Resource
    private RzLogService rzLogService;


    @PostMapping("/list")
    @ApiOperation("监控系统列表")
    @ResponseBody
    public Map<String, Object> jkxtList(@RequestParam Integer page, @RequestParam Integer limit,
                                         @RequestParam(required = false) String begintime,
                                         @RequestParam(required = false) String endtime,
                                         @RequestParam(required = false) String badw,
                                         @RequestParam(required = false) String jk_name) {
        Map<String, Object> result = new HashMap<>();
        Page<Jkxt> p = jkxtService.getInfo(page,limit,begintime,endtime,badw,jk_name);
        result.put("code","0");
        result.put("msg","成功");
        result.put("count",p.getTotal());
        result.put("data",p.getRecords());

        return result;
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "前端摄像机保存页面")
    @ResponseBody
    public ResultFormat jkxtSave(HttpServletRequest request, Jkxt jkxt){
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
        RzLog rzLog = new RzLog();

        ResultFormat insert = jkxtService.insert(jkxt);
        SysUser user = currentUser();
        if (insert.getCode() == 200) {
            rzLog.setRz_user_id(user.getUserId().intValue());
            rzLog.setRz_type(Constants.INSERT);
            rzLog.setRz_msg(Messages.JKXT_ADD_SUCCESS);
            rzLog.setRz_state(Constants.LOG_OK);
            rzLogService.insert(rzLog);
            return ResultUtil.success();
        }
            rzLog.setRz_user_id(user.getUserId().intValue());
            rzLog.setRz_type(Constants.INSERT);
            rzLog.setRz_msg(Messages.JKXT_ADD_Fail);
            rzLog.setRz_state(Constants.LOG_NO);
            rzLogService.insert(rzLog);

        return ResultUtil.error(100, "添加失败");

    }

    @ApiOperation("监控系统删除")
    @PostMapping("/delete")
    @ResponseBody
    public Integer jkxtDelete(HttpServletRequest request, @RequestParam String jk_id) {
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
        RzLog rzLog = new RzLog();
        try {
            SysUser user = currentUser();
            int i = jkxtService.deleteById(Integer.parseInt(jk_id));
            if (i > 0) {
                rzLog.setRz_user_id(user.getUserId().intValue());
                rzLog.setRz_type(Constants.DELETE);
                rzLog.setRz_msg(Messages.JKXT_DELETE_SUCCESS);
                rzLog.setRz_state(Constants.LOG_OK);
                rzLogService.insert(rzLog);
                //正常
                return 0;
            }else {
                rzLog.setRz_user_id(user.getUserId().intValue());
                rzLog.setRz_type(Constants.DELETE);
                rzLog.setRz_msg(Messages.JKXT_DELETE_Fail);
                rzLog.setRz_state(Constants.LOG_NO);
                rzLogService.insert(rzLog);
                //异常
                return -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @PostMapping(value = "/update")
    @ApiOperation("监控系统更新")
    @ResponseBody
    public void jkxtUpdate(Jkxt jkxt){
        RzLog rzLog = new RzLog();
        int i = jkxtService.updateJkxt(jkxt);
        SysUser user = currentUser();
        if (i>0) {
            rzLog.setRz_user_id(user.getUserId().intValue());
            rzLog.setRz_type(Constants.UPDATE);
            rzLog.setRz_msg(Messages.JKXT_UPDATE_SUCCESS);
            rzLog.setRz_state(Constants.LOG_OK);
            rzLogService.insert(rzLog);
        }else {
            rzLog.setRz_user_id(user.getUserId().intValue());
            rzLog.setRz_type(Constants.UPDATE);
            rzLog.setRz_msg(Messages.JKXT_UPDATE_FAIL);
            rzLog.setRz_state(Constants.LOG_NO);
            rzLogService.insert(rzLog);
        }
    }
}
